package com.atguigu.eduservice.controller.index;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.front.CourseFrontVo;
import com.atguigu.eduservice.entity.front.CourseWebVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.queryVo.CourseOrderVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

//@CrossOrigin
@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {


    @Autowired
    private EduCourseService courseService;


    @Autowired
    private EduChapterService chapterService;


    @ApiOperation(value = "条件查询分页课程")
    @PostMapping("getFrontCourseList/{current}/{limit}")
    public R getFrontCourseList(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) CourseFrontVo courseFrontVo){

        Page<EduCourse> page = new Page<>(current, limit);
        Page<EduCourse> courseList  =  courseService.getCourseFrontList(page, courseFrontVo);



        return  R.ok().data(courseList);
    }

    //根据课程id，编写sql语句查询课程信息
    @ApiOperation(value = "根据课程id获取课程基本信息")
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R geFrontCourseInfo(@PathVariable  String courseId){

        CourseWebVo coursewebvo =  courseService.getBaseCourseInfo(courseId);

        //根据课程id查询章节和小节
        List<ChapterVo> chapterList =  chapterService.getChapterVideoById(courseId);

        HashMap<String, Object> map = new HashMap<>();
        map.put("courseWebVo", coursewebvo);
        map.put("chapterVideoList", chapterList);

        return R.ok().data(map);

    }

    @ApiOperation(value = "根据课程id查询课程信息")
    @PostMapping("getCourseInfoOrder/{courseId}")
    public CourseOrderVo  getCourseInfoOrder(@PathVariable String courseId){

        EduCourse course = courseService.getById(courseId);
        CourseOrderVo courseOrderVo = new CourseOrderVo();
        BeanUtils.copyProperties(course, courseOrderVo);

        return courseOrderVo;

    }

}
